﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Interface.Pharmacy;
using Application.Common;
using Application.Presentation;
using Interface.Administration;

namespace Touch
{
    public partial class frmprintdialogPASDP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            int PatientId = Convert.ToInt32(Request.QueryString["ptnpk"]);
            int VisitId = Convert.ToInt32(Request.QueryString["VisitId"]);

            IPediatric PrintManager;
            PrintManager = (IPediatric)ObjectFactory.CreateInstance("BusinessProcess.Pharmacy.BPediatric, BusinessProcess.Pharmacy");
            DataSet theDSPrint = PrintManager.GetPharmacyDetailforLabelPrint(PatientId, VisitId);

            const string sDosageTemplate = "<DRUGNAME> <br />Dose: <DOSE> Freq: <FREQ> Duration: <DURATION> Qty: <DRUGQUANTITY><br /><br />";

            UserControl uc = (UserControl)Page.LoadControl("usrctrlprintpharmacy.ascx");
            //uc.ID = "" + i + "";
            Label lblFacility = ((Label)uc.FindControl("lblfacility"));
            lblFacility.Text = Convert.ToString(theDSPrint.Tables[0].Rows[0]["FacilityName"]);
            //lblFacility.ID = "fac" + i + "";
            Label lblpName = ((Label)uc.FindControl("lblpName"));
            lblpName.Text = "Patient Name: " + Convert.ToString(theDSPrint.Tables[0].Rows[0]["Name"]) + "<br /><br />Folder No:" + theDSPrint.Tables[0].Rows[0]["D9FolderNo"].ToString() + "<br />";
            //lblpName.ID = "pname" + i + "";

            string sDrugText = "";
            int i = 0;
            foreach (DataRow DR in theDSPrint.Tables[1].Rows)
            {
                sDrugText = sDrugText + sDosageTemplate;
                sDrugText = sDrugText.Replace("<DRUGNAME>", DR["DrugName"].ToString());
                sDrugText = sDrugText.Replace("<DOSE>", DR["SingleDose"].ToString());
                sDrugText = sDrugText.Replace("<FREQ>", DR["Frequency"].ToString());
                sDrugText = sDrugText.Replace("<DURATION>", DR["Duration"].ToString());
                sDrugText = sDrugText.Replace("<DRUGQUANTITY>", DR["OrderedQuantity"].ToString());
                sDrugText = sDrugText.Replace("<CLINICIAN>", DR["Clinician"].ToString());
                sDrugText = sDrugText.Replace("<ORDERDATE>", DR["OrderDate"].ToString());
            }
            sDrugText = sDrugText + "By: <CLINICIAN> Date: <ORDERDATE><br />";
            sDrugText = sDrugText.Replace("<CLINICIAN>", theDSPrint.Tables[1].Rows[0]["Clinician"].ToString());
            sDrugText = sDrugText.Replace("<ORDERDATE>", theDSPrint.Tables[1].Rows[0]["OrderDate"].ToString());

                Label lbldrugName = ((Label)uc.FindControl("lbldrugName"));
                lbldrugName.Text = sDrugText; //lbldrugName.Text + Convert.ToString(DR["DrugName"]);
                
                TextBox txtprintInstruction = ((TextBox)uc.FindControl("txtprintInstruction"));
                //txtprintInstruction.Text = Convert.ToString(DR["PatientInstructions"]);
                txtprintInstruction.ID = "PIns" + i + "";
                Button theButton = new Button();
                theButton.ID = "btn1" + i + "";
                theButton.Text = "Print Label";
                theButton.Click += new EventHandler(theButton_Click);
                Panel thepnl = new Panel();
                thepnl.ID = "pnl" + i + "";
                thepnl.BorderStyle = BorderStyle.Double;
                thepnl.BorderColor = System.Drawing.Color.Black;
                thepnl.Controls.Add(uc);
                //thepnl.Controls.Add(theButton);
                thepnl.Controls.Add(new LiteralControl("<br /><br />"));
                pnlprintdialog.Controls.Add(thepnl);
                i++;
            
        }

        void theButton_Click(object sender, EventArgs e)
        {
            string m = ((Button)sender).ID.Replace("btn1", "pnl");
            string script = "<script language = 'javascript' defer ='defer' id = 'confirm'>\n";
            script += "fnprint('"+m+"');\n";
            script += "</script>\n";
            RegisterStartupScript("confirm", script);
        }

        protected void btnprint_Click(object sender, EventArgs e)
        {
            string m = ((Button)sender).ID.Replace("btn1", "pnl");
            string script = "<script language = 'javascript' defer ='defer' id = 'confirm'>\n";
            script += "doPrint();\n";
            script += "</script>\n";
            RegisterStartupScript("confirm", script);
        }

       
    }
}